<template>
  <div class="unavailable-logs">
    <el-table
      v-loading="listLoading"
      :data="list"
      element-loading-text="Loading"
      fit
      highlight-current-row>
      <el-table-column label="节点ID">
        <template slot-scope="scope">
          {{ scope.row.node_id }}
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          {{ scope.row.action }}
        </template>
      </el-table-column>
      <el-table-column label="操作前值">
        <template slot-scope="scope">
          {{ scope.row.value_before ? scope.row.value_before : '-' }}
        </template>
      </el-table-column>
      <el-table-column label="操作后值">
        <template slot-scope="scope">
          {{ scope.row.value_after ? scope.row.value_after : '-' }}
        </template>
      </el-table-column>
      <el-table-column label="操作人">
        <template slot-scope="scope">
          {{ scope.row.operator ? scope.row.operator : '-' }}
        </template>
      </el-table-column>
      <el-table-column
        label="操作时间">
        <template slot-scope="scope">
          <span>{{ scope.row.action_time ? new Date(Date.parse(new Date(scope.row.action_time))).Format("yyyy-MM-dd hh:mm:ss") : '-' }}</span>
        </template>
      </el-table-column>
      <el-table-column label="备注">
        <template slot-scope="scope">
          {{ scope.row.remark }}
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
import { getLogList } from '@/api/source'
import { mapGetters } from 'vuex'

export default {
  name: 'Dashboard',
  data() {
    return {
      listLoading: true,
      list: []
    }
  },
  computed: {
    ...mapGetters([
      'curSourceCluster'
    ])
  },
  created() {
    this._fetchData()
  },
  methods: {
    // 二进制转换
    convertBinary(val) {
      if (val === null) {
        return false
      }
      let gpuStr = ''
      const binay = val.toString(2)
      for (let i = 0; i < binay.length; i++) {
        if ((1 << i) & val) {
          gpuStr += i.toString()
        }
      }
      return gpuStr
    },
    // 查询日志列表
    _fetchData() {
      this.listLoading = true
      const param = `?node_id=${this.curSourceCluster.node_id}`
      getLogList(param).then(res => {
        if (res.message.code === 0) {
          this.listLoading = false
          this.list = res.data
          this.list.map(item => {
            item.value_after = this.convertBinary(item.value_after)
            item.value_before = this.convertBinary(item.value_before)
          })
        } else {
          this.$message({ message: res.message.message, showClose: true, type: 'error' })
        }
      })
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
.unavailable-logs {
  margin: 30px;
  .table-action {
    color: #4285f4;
  }
  .grafana-iframe {
    width: 100%;
    min-height: calc(100vh - 167px);
  }
}
</style>
